package com.yunhui.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunhui.entity.Merchant;
import com.yunhui.entity.RouteJb;
import com.yunhui.entity.RouteJbEs;
import com.yunhui.mapper.RouteJbMapper;
import com.yunhui.service.RouteJbService;
import com.yunhui.util.PageSupport;
import org.apache.velocity.shaded.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  行程的基本前端控制层
 * </p>
 *
 * @author WGH
 * @since 2019-12-09
 */
@Controller
@RequestMapping("/routeJb")
public class RouteJbController {
    @Autowired
    private RouteJbService routeJbService;

    /**
     * 测试方法
     * @return
     */
    @RequestMapping("routeJbList")
    public String getList(HttpSession session){
        session.setAttribute("name",2);
        return "pack";
    }

    /**
     * 测试方法
     * @param
     * @return
     */
    @Resource
    
    private RouteJbMapper routeJbElasticsearch;

    @RequestMapping("path")
    @ResponseBody
    public Object path(String title){

		List<RouteJb> list=routeJbElasticsearch.selectList(new QueryWrapper<RouteJb>().like("title",title).like("destination", title).like("lightspot", title));
        return list;
    }


    @RequestMapping("toes")
    public String toPackEs(Model model,
                           String title,String travelType1,String destination ,String min,String max,String index,String pai){
        String ti=null;
        Integer type=null;
        String des=null;
        Integer xiao=null;
        Integer da=null;
        Integer ind=1;
        Integer p=0;

        if(!"null".equals(title)&&title!=null&&!"".equals(title))ti=title;
        if(!"null".equals(travelType1)&&travelType1!=null&&!"".equals(travelType1))type=Integer.valueOf(travelType1);
        if(!"null".equals(destination)&&destination!=null&&!"".equals(destination))des=destination;
        if(!"null".equals(min)&&min!=null&&!"".equals(min))xiao=Integer.valueOf(min);
        if(!"null".equals(min)&&min!=null&&!"".equals(min))da=Integer.valueOf(max);
        if(!"null".equals(index)&&index!=null&&!"".equals(index))ind=Integer.valueOf(index);
        if(!"null".equals(pai)&&pai!=null&&!"".equals(pai))p=Integer.valueOf(pai);

        PageSupport page=new PageSupport();
        page.setCurrentPageNo(ind);
        page.setPageSize(5);
        List<RouteJbEs> list=routeJbService.findByEs(ti,des,type,page,xiao,da,p);

        model.addAttribute("list",list);
        //条件回显
        model.addAttribute("page",page);
        model.addAttribute("title",ti);
        model.addAttribute("travelType1",type);
        model.addAttribute("destination",des);
        model.addAttribute("min",xiao);
        model.addAttribute("max",da);
        model.addAttribute("pai",p);
        return "pack";
    }


    /**
     * 跳转至目的地参团
     * @param model
     * @return
     */
    @RequestMapping("mddct")
    public String toMddct(Model model){

        RouteJb routeJb=new RouteJb();
        routeJb.setStatus(2);
        PageSupport page=new PageSupport();
        page.setPageSize(3);
        page.setCurrentPageNo(1);
        //飞机参团游
        routeJb.setTravelType2(30);
        List<RouteJb> flist=routeJbService.getPageRouteJb(routeJb,page);
        model.addAttribute("flist",flist);
        //目的地参团游
        routeJb.setTravelType2(31);
        List<RouteJb> mlist=routeJbService.getPageRouteJb(routeJb,page);
        model.addAttribute("mlist",mlist);
        //出发地参团游
        routeJb.setTravelType2(32);
        List<RouteJb> clist=routeJbService.getPageRouteJb(routeJb,page);
        model.addAttribute("clist",clist);
        return "localjoin";
    }



    /**
     * 跳转至商家后台行程列表
     * @param title
     * @param status
     * @param index
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("shanglist")
    public String toShangList(
            String title,String status,String index,String biao,
            HttpSession session,
            Model model
    ){
        if("0".equals(biao)){
            model.addAttribute("gigu","提交失败！");
        }
        if("1".equals(biao)){
            model.addAttribute("gigu","提交成功！");
        }
        if("3".equals(biao)){
            model.addAttribute("gigu","删除成功！");
        }
        if("4".equals(biao)){
            model.addAttribute("gigu","删除失败！");
        }
        if("5".equals(biao)){
            model.addAttribute("gigu","修改成功！");
        }
        if("10".equals(biao)){
            model.addAttribute("gigu","添加成功！");
        }

        int ind=0;
        if(index!=null&&!"".equals(index)){
            ind=Integer.parseInt(index);
        }
        if(ind ==0){
            ind=1;
        }
        RouteJb routeJb =new RouteJb();
        if(!"null".equals(title)&&title!=null&&!"".equals(title))routeJb.setTitle(title);
        if(!"null".equals(status)&&status!=null&&!"".equals(status))routeJb.setStatus(Integer.valueOf(status));
        Merchant merchant =  (Merchant)session.getAttribute("merchant");
        routeJb.setmId(merchant.getId());
        PageSupport page=new PageSupport();
        page.setCurrentPageNo(ind);
        page.setPageSize(5);
        page.setTotalCount(routeJbService.getCount(routeJb));

        List<RouteJb> list=routeJbService.getPageRouteJb(routeJb,page);

        model.addAttribute("routeJb",routeJb);
        model.addAttribute("page",page);
        model.addAttribute("list",list);

        return "shang/shangjb-list";
    }


    //跳转至后台行程列表
    @RequestMapping("houtailist")
    public String toList(Model model,
                         String title,String status,String mId,String proName,
                         String index
                        ){
        int ind=0;
        if(index!=null&&!"".equals(index)){
            ind=Integer.parseInt(index);
        }
        if(ind ==0){
            ind=1;
        }
        RouteJb routeJb =new RouteJb();
        if(!"null".equals(title)&&title!=null&&!"".equals(title))routeJb.setTitle(title);
        if(!"null".equals(proName)&&proName!=null&&!"".equals(proName))routeJb.setProName(proName);
        if(!"null".equals(status)&&status!=null&&!"".equals(status))routeJb.setStatus(Integer.valueOf(status));
        if(!"null".equals(mId)&&mId!=null&&!"".equals(mId))routeJb.setmId(Integer.valueOf(mId));

        PageSupport page=new PageSupport();
        page.setCurrentPageNo(ind);
        page.setPageSize(5);
        page.setTotalCount(routeJbService.getCount(routeJb));

        List<RouteJb> list=routeJbService.getPageRouteJb(routeJb,page);

        model.addAttribute("routeJb",routeJb);
        model.addAttribute("page",page);
        model.addAttribute("list",list);
        return "houtai/route-list";
    }

    //跳转至添加页面
    @RequestMapping("insertjb")
    public String toSet(){
        return "shang/admin-add";
    }

    //跳转至修改页面
    @RequestMapping("tosetjb")
    public String toSetJb(
            Integer id,
            Model model
    ){
        RouteJb routeJb=routeJbService.selectByIds(id);
        model.addAttribute("routeJb",routeJb);
        model.addAttribute("so",routeJb.getTravelType1());
        model.addAttribute("st",routeJb.getTravelType2());

        return "shang/setroutejb";
    }




    /**
     * 添加操作
     * */
    @RequestMapping("addjb")
    public String addJb(@RequestParam("uploadFile") MultipartFile[] uploadFile,
                        HttpSession session,
                        RouteJb routeJb,
                        Model model
                        ){
        String[] names=getName(uploadFile);
        if(names==null){
            model.addAttribute("gigu","图片上传出错！");
            return "shang/admin-add";
        }
        routeJb.setImgName1(names[0]);
        routeJb.setImgName2(names[1]);
        routeJb.setImgName3(names[2]);
        routeJb.setImgName4(names[3]);
        routeJb.setmId(Integer.valueOf(session.getAttribute("name").toString()));
        routeJb.setNight(routeJb.getDays()-1);
        routeJb.setCreateDate(new Date());// new Date()为获取当前系统时间
        int i=0;
        try {
            i=routeJbService.insert(routeJb);
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("gigu","添加失败！");
            return "shang/admin-add";
        }
        if(i>0){
            return "redirect:/routeJb/shanglist?biao=10";
        }else{
            model.addAttribute("gigu","添加失败！");
            return "shang/admin-add";
        }

    }



    /**
     * 修改操作
     * @param uploadFile
     * @param model
     * @param routeJb
     * @return
     */
    @RequestMapping("updateJb")
    public String updateJb(
            @RequestParam("uploadFile") MultipartFile[] uploadFile,
            Model model,
            RouteJb routeJb
    ){
        System.err.println(routeJb.toString());
        String[] names=getName(uploadFile);
        if(names==null){
            model.addAttribute("gigu","图片上传出错！");
            return "routeJb/tosetjb?id="+routeJb.getId();
        }
        routeJb.setImgName1(names[0]);
        routeJb.setImgName2(names[1]);
        routeJb.setImgName3(names[2]);
        routeJb.setImgName4(names[3]);
        routeJb.setNight(routeJb.getDays()-1);
        int i=0;
        try {
            i=routeJbService.updateByIdQun(routeJb);
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("gigu","修改失败！");
            return "routeJb/tosetjb?id="+routeJb.getId();
        }
        if(i>0){
            return "redirect:/routeJb/shanglist?biao=5";
        }else{
            model.addAttribute("gigu","修改失败！");
            return "routeJb/tosetjb?id="+routeJb.getId();
        }
    }







    /**
     * ajxa修改行程状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("routelist-json")
    @ResponseBody
    public Object retJson (
            Integer id,
            Integer status
    ){
        int num =routeJbService.updateById(id,status);

        return num;
    }

    /**
     * ajax加载行程
     * @return
     */
//    @RequestMapping("/twoge")
//    @ResponseBody
//    public Object twoge (
//            Integer size
//    ){
//        System.err.println(123);
//        RouteJb routeJb=new RouteJb();
//        routeJb.setStatus(2);
//        PageSupport page=new PageSupport();
//        page.setPageSize(size);
//        page.setCurrentPageNo(1);
//        List<RouteJb> list=routeJbService.getPageRouteJb(routeJb,page);
//        return "list";
//    }



    /**
     * 重新提交
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("setstatus")
    public String setStatus(
            Integer id,
            Integer status
    ){
        int num =routeJbService.updateById(id,status);
        if(num>0){
            return "redirect:/routeJb/shanglist?biao=1";
        }
        return "redirect:/routeJb/shanglist?biao=0";
    }


    /**
     * 删除行程
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public String delete(Integer id){

        int num=routeJbService.deleteById(id);

        if(num>0){
            return "redirect:/routeJb/shanglist?biao=3";
        }
        return "redirect:/routeJb/shanglist?biao=4";

    }



    //储存图片返回地址
    public String[] getName(MultipartFile[] uploadFile){
        String [] nameList=new String[4];
        String path=null;
        int ind=0;
        for (MultipartFile mu:uploadFile
             ) {
            //System.out.println(1);
            if (mu!=null){
                //定义文件促存放的文件夹
                //D:\RuanJianGongJu\idea\idea 2019\ideaWork\travel\target\classesstatic/images/
                try {

                    path=new File("").getCanonicalPath()+"/target/classes/static/images";
                }catch (IOException e){
                    e.printStackTrace();
                }
                //获取上传的文件名
                String imgName=mu.getOriginalFilename();
                //System.out.println(imgName);
                //获取文件后缀名
                String suffix= FilenameUtils.getExtension(imgName);

                //根据后缀名判断是否是图片
                if(suffix.equalsIgnoreCase("png")
                        ||suffix.equalsIgnoreCase("jpg")
                        ||suffix.equalsIgnoreCase("jpeg")
                        ||suffix.equalsIgnoreCase("pneg")) {
                    //设置上传后的文件存放名
                    //1~1000的随机数+当前时间毫秒数+后缀名
                    int z = (int) (Math.random() * 900) + 100;//z的范围[100~999]
                    String filePath = z + "sd" + System.currentTimeMillis() + "." + suffix;
                    //静态文件路径
                    String imgPath = "/images/" + filePath;

                    File file = new File(path, filePath);
                    //判断文件是否存在
                    if (!new File(path).exists()) {
                        new File(path).mkdirs();//不存在就创建
                    }
                    try {
                        mu.transferTo(file);  //将上传过来的文件通过创建的IO流存入
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    nameList[ind] = imgPath;
                    ind++;

                }else if("".equals(suffix)){
                    nameList[ind] = null;
                    ind++;
                }else{
                    return null;
                }


            }
        }
        return nameList;
    }
}

